package com.easy.work.function.main.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.syslib.util.TextUtil;
import com.base.syslib.view.XCFlowLayout;
import com.easy.work.R;
import com.easy.work.bean.WorkBaseInfo;
import com.easy.work.utils.StringUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/12/11.
 * 招聘需求列表
 */
public class MainListAdapter extends BaseAdapter {

    private Context mCtx;
    MarginLayoutParams marginParams;
    List<WorkBaseInfo> workBaseInfoList = new ArrayList<>();

    public MainListAdapter(Context context, List<WorkBaseInfo> workBaseInfoList) {
        this.mCtx = context;
        this.workBaseInfoList = workBaseInfoList;
    }

    @Override
    public int getCount() {
        return workBaseInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return workBaseInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mCtx).inflate(R.layout.item_recruitment, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);


            ViewGroup.LayoutParams params = viewHolder.rlXCFlowLayout.getLayoutParams();
            //获取view的margin设置参数
            if (params instanceof MarginLayoutParams) {
                marginParams = (MarginLayoutParams) params;
            } else {
                //不存在时创建一个新的参数
                //基于View本身原有的布局参数对象
                marginParams = new MarginLayoutParams(params);
            }
            marginParams.rightMargin = 20;
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        WorkBaseInfo bean = workBaseInfoList.get(position);
//        String[] data = new String[]{"包吃", "包住", "五险一金"};
        String[] data = bean.welfareList;
        if (data != null && data.length > 01) {
            viewHolder.welfareType.removeAllViews();
            for (int i = 0; i < data.length; i++) {
                final TextView view = new TextView(mCtx);
                view.setTextSize(mCtx.getResources().getDimension(R.dimen.xcflowlayout_tv_size));
                view.setBackground(mCtx.getResources().getDrawable(R.drawable.corner_bg_gray));
                view.setTextColor(mCtx.getResources().getColor(R.color.gray_bg_999));
                view.setGravity(Gravity.CENTER);
                view.setText(data[i]);

                viewHolder.welfareType.addView(view, marginParams);
            }
        }

        initView(viewHolder, position);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, int position) {
        WorkBaseInfo bean = workBaseInfoList.get(position);
        if (bean == null) {
            return;
        }
        if (StringUtil.isNotEmpty(bean.companyImage)) {
            Picasso.with(mCtx)
                    .load(bean.companyImage)
                    .error(R.mipmap.default_img)
                    .into(viewHolder.imgCompany);
        } else {
            Picasso.with(mCtx)
                    .load(bean.workImage)
                    .error(R.mipmap.default_img)
                    .into(viewHolder.imgCompany);
        }

        if (TextUtil.isNotNull(bean.title))
            viewHolder.tvTitle.setText(bean.title);
        viewHolder.tvWage.setText(bean.salaryFrom + "-" + bean.salaryTo + "元/月");
        if (bean.isHot == 1) {
            viewHolder.imgIsHot.setVisibility(View.VISIBLE);
        } else {
            viewHolder.imgIsHot.setVisibility(View.GONE);
        }
        if (bean.isCashBack == 1) {
            viewHolder.llFan.setVisibility(View.VISIBLE);
//            viewHolder.tvCashBack.setText("入职三个月，返现金" + bean.cashBackNum + "元");
            viewHolder.tvCashBack.setText(bean.cashBackDes);
        } else {
            viewHolder.llFan.setVisibility(View.GONE);
        }
        if (bean.isRecPrize == 1) {
            viewHolder.llJiang.setVisibility(View.VISIBLE);
//            viewHolder.tvRecPrize.setText("推荐入职，送现金" + bean.recPrizeNum + "元");
            viewHolder.tvRecPrize.setText(bean.recPrizeDes);
        } else {
            viewHolder.llJiang.setVisibility(View.GONE);
        }
        if (TextUtil.isNull(bean.areaText) || TextUtil.isNull(bean.distance)) {
            viewHolder.tvDistance.setText("");
        } else {
            viewHolder.tvDistance.setText(bean.areaText + "<" + bean.distance);
        }
    }


    class ViewHolder {
        @BindView(R.id.img_company)
        ImageView imgCompany;
        @BindView(R.id.img_is_hot)
        ImageView imgIsHot;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.welfare_type)
        XCFlowLayout welfareType;
        @BindView(R.id.rl_XCFlowLayout)
        RelativeLayout rlXCFlowLayout;
        @BindView(R.id.tv_wage)
        TextView tvWage;
        @BindView(R.id.tv_cash_back)
        TextView tvCashBack;
        @BindView(R.id.ll_fan)
        LinearLayout llFan;
        @BindView(R.id.tv_rec_prize)
        TextView tvRecPrize;
        @BindView(R.id.ll_jiang)
        LinearLayout llJiang;
        @BindView(R.id.tv_distance)
        TextView tvDistance;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
